package com.buddy.syn.block._12;

/**
 * 在JVM中具有String常量池缓存功能
 * 因此，如果使用同一个String，容易导致持有同一把锁
 * 因此通常不适用String作为锁对象，而是以new Object替代
 */
public class StringAndSynService {

    public static void print(String stringParam) {
        try {
            synchronized (stringParam) {
                while (true) {
                    System.out.println("Thread Name = " + Thread.currentThread().getName());
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void print(Object object) {
        try {
            synchronized (object) {
                while (true) {
                    System.out.println("object不放入缓存，Thread Name = " + Thread.currentThread().getName());
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
